package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.MediclManDto;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * 预约控制器
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetMealService setMealService;


    @PostMapping("/submit")
    public Result submit(@RequestBody MediclManDto mediclManDto) {
        Order order = orderService.edidcalAppintment(mediclManDto);//定义一个实体类接收前端数据

        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,order);
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        Order order = orderService.findById(id);
        Integer memberId = order.getMemberId();
        String orderType = order.getOrderType();
        Integer setmealId = order.getSetmealId();
        Setmeal setmeal = setMealService.findById2(setmealId);
        Member member = memberService.findById(memberId);
        String name = member.getName();
        //日期转换
        String STANDARD_DATE_FORMAT_UTC = "yyyy/MM/dd/HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_DATE_FORMAT_UTC);//创建日期时间模板
        String date = "";

        date = simpleDateFormat.format(order.getOrderDate());//根据模板把时间格式转换成特定格式的字符串
        //根据键封装map集合 1note:键的值要与实体类对应
        HashMap<String, Object> map = new HashMap<>();
        map.put("setmeal", setmeal.getName());
        map.put("orderType", orderType);
        map.put("member", name);
        map.put("orderDate", date);
        return new Result(true, MessageConst.ACTION_SUCCESS, map);
    }

}